<?php
namespace Common\Model;

use Think\Model;

class UserInterestsModel extends Model
{

    protected $_validate = array(
        array(
            'uid,interests',
            'identicalNull',
            '',
            1,
            'callback'
        )
    );

    protected $_auto = array(
        array(
            'addtime',
            'time',
            1,
            'function'
        )
    ) // 添加时间
;

    public function add_user_interests($data)
    {
        if (false === $this->create($data)) {
            return array(
                'state' => 0,
                'error' => $this->getError()
            );
        } else {
            if (false === $insert_id = $this->add()) {
                return array(
                    'state' => 0,
                    'error' => '数据添加失败！'
                );
            }
        }
        
        return array(
            'state' => 1,
            'id' => $insert_id
        );
    }

    public function save_user_interests($data)
    {
        if (false === $this->create($data)) {
            return array(
                'state' => 0,
                'error' => $this->getError()
            );
        } else {
            if (false === $this->save()) {
                return array(
                    'state' => 0,
                    'error' => '数据添加失败！'
                );
            }
        }
        
        return array(
            'state' => 1,
            'id' => $data['id']
        );
    }
    
    public function isDone($uid) {
        if($this->get_user_interests($uid)!=false)
            return true;
        
         return false;  
    }
    
    public function get_user_interests($uid)
    {
        if($uid) {
            $where['uid'] = array('eq',$uid);
            
            $result = $this->field('id,uid,interests')->where($where)->find();
            if($result) {
                $result['interests'] = json_decode($result['interests'], true);
                return $result;
            }
        }
        
        return false;
		
		
    }
}
?>